package com.icss.web.head.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.icss.common.controller.BaseController;
import com.icss.common.util.PageInfoUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.goods.service.IGoodsDbService;
import com.icss.goods.vo.ReClassify;
import com.icss.goods.vo.ReClassifyEnum;
import com.icss.goods.vo.ReEval;
import com.icss.goods.vo.ReEvalEnum;
import com.icss.goods.vo.ReGoods;
import com.icss.goods.vo.ReGoodsEnum;

/**
 *  首页操作
 * 
 * @author 刘鑫
 */
@Controller
@RequestMapping("/")
public class IndexHeadController extends BaseController
{
	@Resource
	private IGoodsDbService goodsDbService ;
	
	/**
	 * 首页
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/main")
	public String main(HttpServletRequest request, HttpServletResponse response)
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		pageInfoUtil.setPageSize(12);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 必须是上架的商品 */
		condMap.put("status", ReGoodsEnum.STATUS_UP.getStatus());
		/* 按照销量排名的前20件商品 */
		request.setAttribute("orderby", "saleNumDesc");
		
		/* 扩展信息 */
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ReGoods> apiResponse = this.goodsDbService.findCondListGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		/* 页面跳转 */
		return "/head/main";
	}
	
	/**
	 * header
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/header")
	public String header(HttpServletRequest request, HttpServletResponse response)
	{
		/* 查询所有的分类 */
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("status", ReClassifyEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ReClassify> apiResponse = this.goodsDbService.findCondListClassifyService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("headerResponse", apiResponse.toJSON());
		return "/head/header";
	}
	
	/**
	 * 商品列表
	 * @param request
	 * @param response
	 * @return 
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/goodsList")
	public String goodsList(HttpServletRequest request, HttpServletResponse response)
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		pageInfoUtil.setPageSize(12);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 必须是上架的商品 */
		condMap.put("status", ReGoodsEnum.STATUS_UP.getStatus());
		
		/* 根据分类搜索 */
		String classifyId = request.getParameter("classifyId");
		if(classifyId == null)
		{
			classifyId = "" ; 
		}
		request.setAttribute("classifyId", classifyId);
		condMap.put("classifyId", classifyId);

		/* 根据dataType搜索 */
		String dataType = request.getParameter("dataType");
		if(dataType == null)
		{
			dataType = "" ; 
		}
		request.setAttribute("dataType", dataType);
		condMap.put("dataType", dataType);
		
		/* 根据分类搜索 */
		String orderby = request.getParameter("orderby");
		if(orderby == null)
		{
			orderby = "" ; 
		}
		request.setAttribute("orderby", orderby);
		condMap.put("orderby", orderby);
		
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ReGoods> apiResponse = this.goodsDbService.findCondListGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 查询所有的分类 */
		condMap.clear();
		condMap.put("status", ReClassifyEnum.STATUS_ENABLE.getStatus());
		ApiResponse<ReClassify> cateApiResponse = this.goodsDbService.findCondListClassifyService(null, condMap);
		request.setAttribute("cateResponse", cateApiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/goodsList";
	}
	
	/**
	 * 商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/goodsInfo")
	public String goodsInfo(HttpServletRequest request, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<ReGoods> apiResponse = this.goodsDbService.findOneGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		/* 加载评论列表 */
		condMap.clear();
		condMap.put("goodsId", id);
		condMap.put("status", ReEvalEnum.STATUS_ENABLE.getStatus());
		ApiResponse<ReEval> evalApiResponse = this.goodsDbService.findCondListEvalService(null, condMap);
		request.setAttribute("evalApiResponse", evalApiResponse.toJSON());
		return "/head/goodsInfo";
	}
}
